package light.head.dao.el;

import java.util.ArrayList;
import java.util.List;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.el.ClassRoomItem;

import org.springframework.stereotype.Service;

@Service
public class ClassRoomItemDAO extends BaseDAO<ClassRoomItem, Integer> {
	/**
	 *  根据教室编号获取使用明细
	 */
	public List<ClassRoomItem> findByCrc(Integer crc){
		String sql = "FROM ClassRoomItem cri WHERE cri.deleted = 0 AND cri.crc = ?";
		return find(sql, crc);
	}
	/**
	 *  根据教室编号获取使用明细
	 */
	public List<ClassRoomItem> findByOc(List<Integer> ocs){
		StringBuffer sql = new StringBuffer("FROM ClassRoomItem cri WHERE cri.deleted = 0 AND cri.crc IN (SELECT cr.code FROM ClassRoom cr WHERE cr.deleted = 0 AND cr.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			sql.append("?,");
			list.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append("))");
		return find(sql.toString(), list.toArray());
	}
	
	
	public RESULT update(ClassRoomItem item){
		if(item == null || item.getCode() == null) return RESULT.ERROR;
		
		ClassRoomItem old = get(item.getCode());
		
		if(old == null) return RESULT.NOT_FIND;
		
		if(item.getAllDay() != null && item.getAllDay() != old.getAllDay()){
			old.setAllDay(item.getAllDay());
		}
		if(item.getStartTime() != null){
			old.setStartTime(item.getStartTime());
		}
		if(item.getEndTime() != null){
			old.setEndTime(item.getEndTime());
		}
		if(isNotNull(item.getTitle())){
			old.setTitle(item.getTitle());
		}
		if(item.getClc() != null && item.getClc() != old.getClc()){
			old.setClc(item.getClc());
		}
		if(item.getDeleted() != null && item.getDeleted() != old.getDeleted()){
			old.setDeleted(item.getDeleted());
		}
		old.setRemark(item.getRemark());
		return upd(old);
	}
}
